package com.ys.service.business.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.business.TbRepeatWord;
import com.ys.mapper.business.TbRepeatWordMapper;
import com.ys.mapper.sys.SysMapper;
import com.ys.service.business.TbRepeatWordService;
import com.ys.vo.R;

@Service("tbRepeatWordService")
public class TbRepeatWordServiceImpl implements TbRepeatWordService {

	@Autowired
	private SysMapper sysMapper;
	@Autowired
	private TbRepeatWordMapper tbRepeatWordMapper;
	
	@Override
	@Transactional
	public R add(Map<String, Object> params) throws Exception {

		TbRepeatWord entity = JSON.parseObject(JSON.toJSONString(params), TbRepeatWord.class);
		
		String name = params.get("name").toString();
		if (name == null ) {
			return R.error("名称不能为空");
		} else if (name.startsWith(" ") || name.endsWith(" ") || name.startsWith("\t") || name.endsWith("\t")) {
			return R.error("名称起始不能包含空格");
		} else if (new TbRepeatWord().setCountry(entity.getCountry()).setName(name).count() > 0) {
			return R.error("该词汇已存在");
		}

		tbRepeatWordMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	@Transactional
	public R edit(Map<String, Object> params) throws Exception {
		TbRepeatWord entity = JSON.parseObject(JSON.toJSONString(params), TbRepeatWord.class);

		tbRepeatWordMapper.update(entity);
		
		return R.ok("操作成功");
	}

	@Override
	@Transactional
	public R deleteTrue(String guid) {
		if (guid == null || guid.length() == 0) {
			return R.error("ID不能为空");
		}
		
		// 删除角色-按钮
		sysMapper.deleteTrueTbBaseInfoByGuid(guid);
		
		return R.ok();
	}
}
